Date.prototype.format = function(fmt) {
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
}

// $.ajax({
//     url:"/purchase/showPid",
//     type:"post",
//     success:function (data) {
//         var list=data.obj.list
//     }
// })
var vm= new Vue({
    el:"#ljw",
    data:{
        list:"",//采购单号集合
        nameSp:"",//供应商名称集合
        cgdan:"0",//选择得采购单值
        Po:"",
        woseId:0,//仓库ID
        supname:0,//供应商名称
        listPl:[],//退货商品

        jbr:"",//经办人
        thsj:"",//退货时间
        sl:"",
        quantity2:1,
        remark:"",
        heji:0,
        as:"",
        total:"",
        kth:[],
        xgth:{},
    },
    methods:{
        cgd(){
            axios({
                method:"post",
                url:"/purchase/showPid"
            }).then(response=>{

                var listPid=response.data.obj
                vm.list=listPid
            })
        },
        gys(){
            axios({
                method: "post",
                url:"/purchase/nameSp",
            }).then(response=>{
                console.log(response)
                var listname=response.data.obj;
                vm.nameSp=listname
            })

        },
        xuanze(){
            axios.get('/purchase/show1Po', {
                params:{
                    pid:this.cgdan
                }
            }).then(res=>{
                vm.supname=res.data.obj.supname
                vm.woseId=res.data.obj.woseId
                vm.supplierId=res.data.obj.woseId
                this.thqd()
            }).catch(err=>{

            })



        },
        thqd(){
            axios.get('/purchase/listPl',{
                params: {
                    pid:this.cgdan,
                }
            }).then(response=>{
                vm.listPl=response.data.obj;
                for(var i in vm.listPl){
                    vm.listPl[i].quantity2=1

                }


                console.log(response.data)

            })
        },//广泛大锅饭大锅饭高浮雕高浮雕高浮雕
        zhi(pl){
            pl.total=pl.quantity*pl.price
        },
        onin(quantity,kStock,ktuihuo,i){
            this.heji=0
            this.heji=this.heji+vm.listPl[i].total
            vm.listPl[i].total=vm.listPl[i].quantity*vm.listPl[i].price
            if(ktuihuo>kStock){
                if(quantity>kStock) {
                    vm.listPl[i].quantity = kStock
                }else if(quantity2<0){
                    vm.listPl[i].quantity = 1
                }
            }else{
                if(quantity>ktuihuo){
                    vm.listPl[i].quantity = ktuihuo
                }else if(quantity<0){
                    vm.listPl[i].quantity=1
                }
            }
            vm.listPl[i].total=vm.listPl[i].quantity*vm.listPl[i].price
            this.heji=0
            for(var i in vm.listPl){
                this.heji=this.heji+vm.listPl[i].total
            }
        },
        baocun(){
            if(this.jbr!=null && this.jbr!=''){
                if(this.thsj!=null && this.thsj!=''){
                    if(this.cgdan!=0){
                        for(var i in this.listPl){
                            vm.xgth.ktuihuo=listPl[i].ktuihuo-listpl[i].quantity
                            vm.xgth.warseId=listPl[i].warseId
                            vm.xgth.pid=this.cgdan
                            this.kth.push(this.xgth)
                        }
                        var dan={
                            purchaseId:this.cgdan,
                            supplier:this.supname,
                            houseId:this.woseId,
                            price:this.heji,
                            orderSta:1,
                            paySta:10,
                            returnDate:this.thsj,
                            single:getCookie(),
                            createDate:new Date().format("yyyy-MM-dd hh:mm:ss"),
                            remark:this.remark,
                            isDelete:1,
                            paidMoney:"0.00"
                        }
                        var xxxlist=[]
                        for(var i in this.listPl){
                            xxxlist.push(this.listPl[i])
                        }


                        var json={
                            PurchaseReturn:dan,
                            list:xxxlist
                        }
                        console.log(json)
                        var data = new FormData()
                        data.append('json',JSON.stringify(json))
                        data.append("file",$("#wenjian")[0].files[0])
                        axios.post('/purchase/addPr',data,{
                            'Content-Type':'multipart/form-data'
                        }).then(res=>{
                            alert("成功")
                            location.href="采购退货.html"
                        }).catch(err=>{
                            alert("失败")
                        })
                    }else{
                        alert("请选择采购单")
                    }
                }else{
                    alert("请选择退货时间")
                }
            }else{
                alert("请填写经办人")
            }
        }






        // dianji(){
        //     if(){
        //
        //     }
        // }


    },
    mounted(){
        this.cgd();
        this.gys();
    }
})

$(function () {
    $("#wenjian").change(function () {
        $("#tup").attr('src',URL.createObjectURL(this.files[0]))
    })
})